import {
	_decorator,
	Component,
	Node,
	systemEvent,
	SystemEvent,
	EventKeyboard,
	KeyCode,
	log,
	v3,
	RigidBody,
	Vec3,
	math,
	Collider,
	PhysicsSystem,
	System,
	EventMouse,
	v2,
} from "cc";
const { ccclass, property } = _decorator;

enum State {
	idle,
	jump,
}

@ccclass("CameraPlayer")
export class CameraPlayer extends Component {

    @property(Node)
    eyeNode:Node=null;
	
	@property
	speed: number = 6;

    @property
    rotateSpeed:number=3;

	state: State = State.idle;

	inputList: Map<KeyCode, boolean> = new Map();

	onLoad() {
		systemEvent.on(SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
		systemEvent.on(SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

		systemEvent.on(SystemEvent.EventType.MOUSE_UP, this.onMouseUp, this);
		systemEvent.on(
			SystemEvent.EventType.MOUSE_DOWN,
			this.onMouseDown,
			this
		);
		systemEvent.on(
			SystemEvent.EventType.MOUSE_MOVE,
			this.onMouseMove,
			this
		);

		let collider = this.getComponent(Collider);
		collider.on("onCollisionEnter", this.onCollisionEnter, this);
	}

	start() {}

	update(deltaTime: number) {
		this.onMove();
		this.onJump();
		this.onRotate();
	}

	onRotate() {
		let rigidBody = this.node.getComponent(RigidBody);
		// let angular = this.node.eulerAngles as Vec3;
		let angular = v3(0, 0, 0);
		if (this.inputList[KeyCode.KEY_Q]) {
			angular.y += this.rotateSpeed;
		}
		if (this.inputList[KeyCode.KEY_E]) {
			angular.y -= this.rotateSpeed;
		}
		rigidBody.setAngularVelocity(angular);
	}

	onJump() {
		if (this.state == State.jump) {
			return;
		}
		let rigidBody = this.node.getComponent(RigidBody);
		if (this.inputList[KeyCode.SPACE]) {
			rigidBody.applyForce(v3(0, 300, 0));
			this.state = State.jump;
		}
	}

	onMove() {
		let v: Vec3 = v3();
		let rigidBody = this.node.getComponent(RigidBody);
		rigidBody.getLinearVelocity(v);
		v = v3(0, v.y, 0);
		if (this.inputList[KeyCode.ARROW_UP] || this.inputList[KeyCode.KEY_W]) {
			v.z = -this.speed;
		}
		if (
			this.inputList[KeyCode.ARROW_DOWN] ||
			this.inputList[KeyCode.KEY_S]
		) {
			v.z = this.speed;
		}
		if (
			this.inputList[KeyCode.ARROW_LEFT] ||
			this.inputList[KeyCode.KEY_A]
		) {
			v.x = -this.speed;
		}
		if (
			this.inputList[KeyCode.ARROW_RIGHT] ||
			this.inputList[KeyCode.KEY_D]
		) {
			v.x = this.speed;
		}

		if (
			this.inputList[KeyCode.ARROW_RIGHT] ||
			this.inputList[KeyCode.KEY_D]
		) {
			v.x = this.speed;
		}
		//根据当前角度进行旋转,使移动可以根据旋转的角度进行
		Vec3.transformQuat(v, v, this.node.getRotation());
		rigidBody?.setLinearVelocity(v);
	}

	onKeyDown(e: EventKeyboard) {
		this.inputList[e.keyCode] = true;
	}

	onKeyUp(e: EventKeyboard) {
		this.inputList[e.keyCode] = false;
	}

	onMouseUp(e: EventMouse) {}

	onMouseDown(e: EventMouse) {}

	onMouseMove(e: EventMouse) {
		let delta = e.getDelta();
		let angle = this.node.eulerAngles as Vec3;
		let eye = this.eyeNode.eulerAngles as Vec3;
		if (delta.x < 0) {
			angle.y = angle.y + this.rotateSpeed;
		}
		if (delta.x > 0) {
			angle.y = angle.y - this.rotateSpeed;
		}
		if (delta.y < 0) {
			eye.x = eye.x - this.rotateSpeed;
			
		}
		if (delta.y > 0) {
			eye.x = eye.x + this.rotateSpeed;
		}
		//限制y轴
		if(eye.x>90){
			eye.y=90;
		}
		if(eye.y<-90){
			eye.y=-90
		}
		this.node.eulerAngles = v3(angle.x, angle.y, angle.z);
		this.eyeNode.eulerAngles = v3(eye.x,eye.y, eye.z);
	}

	onCollisionEnter() {
		this.state = State.idle;
	}
}
